package com.meitianhui.convenitenbuy.util;

import android.content.Context;
import android.text.TextUtils;

import com.google.gson.reflect.TypeToken;
import com.meitianhui.convenitenbuy.R;
import com.meitianhui.convenitenbuy.module.mine.model.Region;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

/**
 *  Created by amy on 2017/11/13.
 */
public class AddressUtil {

    private  static List<Region> regionList=new ArrayList<>();
    //获取本地省市区
    public static String getRegionData(Context context, String s_a_id) {
        String s_sheng="",s_shi="",s_qu="";
        try{
            if(regionList==null||regionList.size()==0) {
                Type responseType = new TypeToken<List<Region>>() {
                }.getType();
                Object data = GsonParserUtils.getInstance().parserRegionJson(context, R.raw.city_change, responseType);
                List<Region> regions = (List<Region>) data;
                regionList.addAll(regions);
            }
            if(!TextUtils.isEmpty(s_a_id)) {
                String s_id = s_a_id.substring(0, 2);
                for (int i = 0; i < regionList.size(); i++) {
                    Region rg = regionList.get(i);
                    if (rg.getId().startsWith(s_id)) {
                        s_sheng = rg.getValue();
                        List<Region.Children> list_c = rg.getChildren();
                        String ss_id = s_a_id.substring(0, 4);
                        for (int j = 0; j < list_c.size(); j++) {
                            Region.Children r_c = list_c.get(j);
                            if (r_c.getId().startsWith(ss_id)) {
                                s_shi = r_c.getValue();
                                List<Region.Children.ChildClass> list_s = r_c.getChildren();
                                for (int k = 0; k < list_s.size(); k++) {
                                    Region.Children.ChildClass r_c_c = list_s.get(k);
                                    if (r_c_c.getId().equals(s_a_id)) {
                                        s_qu = r_c_c.getValue();
                                        break;
                                    }

                                }
                            }
                        }

                    }
                }


            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return s_sheng+s_shi+s_qu;
    }
    public static String getSSQ(Context context, String all_id) {
        String address_path = "";

        if (!TextUtils.isEmpty(address_path)) {
            String[] d_address_path = address_path.split(",");
            if (d_address_path.length == 4) {
                String path1 = d_address_path[1];
                String path2 = d_address_path[2];
                String path3 = d_address_path[3];
                if (path2.contains(path1)) {
                    return path2 + path3;
                }
                return path1 + path2 + path3;
            } else {
                return address_path;
            }
        } else {
            return getRegionData(context, all_id);
        }
    }

}
